library(tidyverse)
## -- Attaching packages ------------------------------------------------------- tidyverse 1.2.1 --
## <U+221A> ggplot2 2.2.1.9000     <U+221A> purrr   0.2.4     
## <U+221A> tibble  1.4.2          <U+221A> dplyr   0.7.4     
## <U+221A> tidyr   0.8.0          <U+221A> stringr 1.3.0     
## <U+221A> readr   1.1.1          <U+221A> forcats 0.3.0
## -- Conflicts ---------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## x dplyr::vars()   masks ggplot2::vars()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(rgdal)
## Warning: package 'rgdal' was built under R version 3.4.4
## Loading required package: sp
## rgdal: version: 1.2-18, (SVN revision 718)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.1.3, released 2017/20/01
##  Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rgdal/gdal
##  GDAL binary built with GEOS: FALSE 
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rgdal/proj
##  Linking to sp version: 1.2-7
library(spatstat)
## Loading required package: spatstat.data
## Loading required package: nlme
## 
## Attaching package: 'nlme'
## The following object is masked from 'package:dplyr':
## 
##     collapse
## Loading required package: rpart
## 
## spatstat 1.55-0       (nickname: 'Stunned Mullet') 
## For an introduction to spatstat, type 'beginner'
## 
## Note: spatstat version 1.55-0 is out of date by more than 10 weeks; a newer version should be available.
library(sf)
## Warning: package 'sf' was built under R version 3.4.4
## Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
library(viridis)
## Loading required package: viridisLite
# Load hex.sent
load("plots/hex.sent") # Twitter / Instagram hex level data
load("plots/twitter.pa.sent") # Twitter planning area level data
load("plots/insta.pa.sent") # Instagram planning area level data
load("plots/richpoor.sent") # Rich / poor data
p <- ggplot() +
  geom_sf(data = twitter.pa.sent, aes(fill = cut(norm, c(0,0.25,0.3,0.35,0.4,0.45)), geometry = geometry, text = paste0(Name, "\n", "Sentiment: ", norm)), lwd = 0) + 
  theme_void() +
  coord_sf() +
  scale_fill_viridis(
    breaks=c(0,0.25,0.3,0.35,0.4,0.45),
    name="Normalized Sentiment",
    discrete = T,
    guide=guide_legend(
      keyheight = unit(3, units = "mm"),
      keywidth=unit(12, units = "mm"),
      label.position = "bottom",
      title.position = 'top',
      nrow=1)) +
  labs(
    title = "Sentiments of Singaporeans in Different Planning Zones",
    subtitle = "Normalized Sentiment score = (Positive - Negative)/Total Count",
    caption = "Data: Ate Poorthuis | Creation: Dragon Minions"
  ) +
  theme(
    text = element_text(color = "#22211d"),
    plot.background = element_rect(fill = "#f5f5f2", color = NA),
    panel.background = element_rect(fill = "#f5f5f2", color = NA),
    legend.background = element_rect(fill = "#f5f5f2", color = NA),
    plot.title = element_text(size= 22, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
    plot.subtitle = element_text(size= 17, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.43, l = 2, unit = "cm")),
    plot.caption = element_text( size=12, color = "#4e4d47", margin = margin(b = 0.3, r=-99, unit = "cm") ),
    legend.position = c(0.7, 0.09),
    panel.grid.major = element_line(colour = 'transparent'),
    panel.grid.minor = element_line(colour = 'transparent')
  )
## Warning: Ignoring unknown aesthetics: text
p

#ggplotly(p, tooltip = "text") %>%
#  highlight(
#    "plotly_hover",
#    opacityDim = 1
#  )
p <- ggplot() +
  geom_sf(data = insta.pa.sent, aes(fill = cut(norm, c(0,0.25,0.4,0.415,0.43,0.46,0.5)), geometry = geometry, text = paste0(Name, "\n", "Sentiment: ", norm)), lwd = 0) + 
  theme_void() +
  coord_sf() +
  scale_fill_viridis(
    breaks=c(0,0.25,0.4,0.415,0.43,0.46),
    name="Normalized Sentiment",
    discrete = T,
    guide=guide_legend(
      keyheight = unit(3, units = "mm"),
      keywidth=unit(12, units = "mm"),
      label.position = "bottom",
      title.position = 'top',
      nrow=1)) +
  labs(
    title = "Instagram Sentiments of Singaporeans in Different Planning Zones",
    subtitle = "Normalized Sentiment score = (Positive - Negative)/Total Count",
    caption = "Data: Ate Poorthuis | Creation: Dragon Minions"
  ) +
  theme(
    text = element_text(color = "#22211d"),
    plot.background = element_rect(fill = "#f5f5f2", color = NA),
    panel.background = element_rect(fill = "#f5f5f2", color = NA),
    legend.background = element_rect(fill = "#f5f5f2", color = NA),
    plot.title = element_text(size= 22, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
    plot.subtitle = element_text(size= 17, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.43, l = 2, unit = "cm")),
    plot.caption = element_text( size=12, color = "#4e4d47", margin = margin(b = 0.3, r=-99, unit = "cm") ),
    legend.position = c(0.7, 0.09),
    panel.grid.major = element_line(colour = 'transparent'),
    panel.grid.minor = element_line(colour = 'transparent')
  )
## Warning: Ignoring unknown aesthetics: text
p

#ggplotly(p, tooltip = "text") %>%
#  highlight(
#    "plotly_hover",
#    opacityDim = 1
#  )
p <- ggplot() +
  geom_sf(data = hex.sent, aes(fill=t.norm, geometry=geometry, text=paste0("Sentiment: ", t.norm)), lwd=0) + 
  theme_void() +
  coord_sf() +
  scale_fill_viridis(
    breaks=c(0,0.25,0.3,0.35,0.4,0.45),
    name="Normalized Sentiment",
    guide=guide_legend(
      keyheight = unit(3, units = "mm"),
      keywidth=unit(12, units = "mm"),
      label.position = "bottom",
      title.position = 'top',
      nrow=1)) +
  labs(
    title = "Twitter Sentiment in Singapore",
    subtitle = "Normalized Sentiment score = (Positive - Negative)/Total Count",
    caption = "Data: Ate Poorthuis | Creation: Dragon Minions"
  ) +
  theme(
    text = element_text(color = "#22211d"), 
    plot.background = element_rect(fill = "#f5f5f2", color = NA), 
    panel.background = element_rect(fill = "#f5f5f2", color = NA), 
    legend.background = element_rect(fill = "#f5f5f2", color = NA),
    plot.title = element_text(size= 22, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
    plot.subtitle = element_text(size= 17, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.43, l = 2, unit = "cm")),
    plot.caption = element_text( size=12, color = "#4e4d47", margin = margin(b = 0.3, r=-99, unit = "cm") ),
    legend.position = c(0.7, 0.09),
    panel.grid.major = element_line(colour = 'transparent'), 
    panel.grid.minor = element_line(colour = 'transparent')
  )
## Warning: Ignoring unknown aesthetics: text
p

#ggplotly(p, tooltip = "text") %>%
#  highlight(
#    "plotly_hover",
#    opacityDim = 1
#  )
p <- ggplot() +
  geom_sf(data = hex.sent, aes(fill=i.norm, geometry=geometry, text=paste0("Sentiment: ", i.norm)), lwd=0) + 
  theme_void() +
  coord_sf() +
  scale_fill_viridis(
    breaks=c(0,0.25,0.3,0.35,0.4,0.45),
    name="Normalized Sentiment",
    guide=guide_legend(
      keyheight = unit(3, units = "mm"),
      keywidth=unit(12, units = "mm"),
      label.position = "bottom",
      title.position = 'top',
      nrow=1)) +
  labs(
    title = "Instagram Sentiment in Singapore",
    subtitle = "Normalized Sentiment score = (Positive - Negative)/Total Count",
    caption = "Data: Ate Poorthuis | Creation: Dragon Minions"
  ) +
  theme(
    text = element_text(color = "#22211d"), 
    plot.background = element_rect(fill = "#f5f5f2", color = NA), 
    panel.background = element_rect(fill = "#f5f5f2", color = NA), 
    legend.background = element_rect(fill = "#f5f5f2", color = NA),
    plot.title = element_text(size= 22, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
    plot.subtitle = element_text(size= 17, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.43, l = 2, unit = "cm")),
    plot.caption = element_text( size=12, color = "#4e4d47", margin = margin(b = 0.3, r=-99, unit = "cm") ),
    legend.position = c(0.7, 0.09),
    panel.grid.major = element_line(colour = 'transparent'), 
    panel.grid.minor = element_line(colour = 'transparent')
  )
## Warning: Ignoring unknown aesthetics: text
p

hex.sent.odds <- hex.sent
hex.sent.odds[hex.sent.odds$odds == 0,]$odds <- NA

p <- ggplot() +
  geom_sf(data = hex.sent, aes(fill=odds, geometry=geometry, text=paste0("Odds Ratio: ", odds)), lwd=0) + 
  theme_void() +
  coord_sf() +
  scale_fill_distiller(
    palette="Spectral",
    limits=c(0,2),
    breaks=c(0,0.3,0.6,1,1.5,2),
    na.value="grey",
    name="Odds Ratio",
    guide=guide_legend(
      keyheight = unit(3, units = "mm"),
      keywidth=unit(12, units = "mm"),
      label.position = "bottom",
      title.position = 'top',
      nrow=1)) +
  labs(
    title = "Sentiment Odds Ratio in Singapore",
    subtitle = "Odds ratio = (Twitter Sentiment) / (Instagram Sentiment)",
    caption = "Data: Ate Poorthuis | Creation: Dragon Minions"
  ) +
  theme(
    text = element_text(color = "#22211d"), 
    plot.background = element_rect(fill = "#f5f5f2", color = NA), 
    panel.background = element_rect(fill = "#f5f5f2", color = NA), 
    legend.background = element_rect(fill = "#f5f5f2", color = NA),
    plot.title = element_text(size= 22, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
    plot.subtitle = element_text(size= 17, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.43, l = 2, unit = "cm")),
    plot.caption = element_text( size=12, color = "#4e4d47", margin = margin(b = 0.3, r=-99, unit = "cm") ),
    legend.position = c(0.7, 0.09),
    panel.grid.major = element_line(colour = 'transparent'), 
    panel.grid.minor = element_line(colour = 'transparent')
  )
## Warning: Ignoring unknown aesthetics: text
p

p <- ggplot(polygons.sf) +
  geom_sf(aes(fill = norm, text = paste0(Name, "\n", "Sentiment: ", norm)), lwd = 0) + 
  theme_void() +
  coord_sf() +
  scale_fill_viridis(
    breaks=c(0,0.25,0.4,0.415,0.43,0.46),
    name="Normalized Sentiment",
    guide=guide_legend(
      keyheight = unit(3, units = "mm"),
      keywidth=unit(12, units = "mm"),
      label.position = "bottom",
      title.position = 'top',
      nrow=1)) +
  labs(
    title = "Sentiments of Singaporeans Living in Different Housing Types",
    subtitle = "Normalized Sentiment score = (Positive - Negative)/Total Count",
    caption = "Data: Ate Poorthuis | Creation: Dragon Minions"
  ) +
  theme(
    text = element_text(color = "#22211d"),
    plot.background = element_rect(fill = "#f5f5f2", color = NA),
    panel.background = element_rect(fill = "#f5f5f2", color = NA),
    legend.background = element_rect(fill = "#f5f5f2", color = NA),
    plot.title = element_text(size= 22, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
    plot.subtitle = element_text(size= 17, hjust=0.01, color = "#4e4d47", margin = margin(b = -0.1, t = 0.43, l = 2, unit = "cm")),
    plot.caption = element_text( size=12, color = "#4e4d47", margin = margin(b = 0.3, r=-99, unit = "cm") ),
    legend.position = c(0.7, 0.09),
    panel.grid.major = element_line(colour = 'transparent'),
    panel.grid.minor = element_line(colour = 'transparent')
  )
## Warning: Ignoring unknown aesthetics: text
ggplotly(p)